package com.hsq.db137.demos.seriver.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hsq.db137.demos.config.JwtUtils;
import com.hsq.db137.demos.config.R;
import com.hsq.db137.demos.entity.Menuinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hsq.db137.demos.mapper.UserinfoMapper;
import com.hsq.db137.demos.entity.Userinfo;
import com.hsq.db137.demos.seriver.UserinfoService;
/**
 * @Author: hsq加油
 * @CreateTime: 2024/12/24-11:48
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class UserinfoServiceImpl extends ServiceImpl<UserinfoMapper, Userinfo> implements UserinfoService{

    @Autowired
    private UserinfoMapper userinfoMapper;

    /**
     * 根据用户ID删除
     * @param id 用户ID
     * @return 提示语句
     */
    @Override
    public String deleteUser(int id) {
        return userinfoMapper.deleteById(id)>0?"删除成功":"删除失败";
    }

    /**
     * 根据用户ID进行修改
     * @param userinfo 用户类
     * @return 提示语句
     */
    @Override
    public String updateUser(Userinfo userinfo) {
        return userinfoMapper.updateById(userinfo)>0?"修改成功":"修改失败";
    }


    /**
     * 用户登录
     * @param username 用户名或者手机号
     * @param password 密码
     * @return 工具类
     */
    @Override
    public R select(String username, String password) {
        QueryWrapper<Userinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("userpass", password)
                .and(i -> i.eq("username", username)
                        .or().eq("phone", username));

        Userinfo userinfo = userinfoMapper.selectOne(wrapper);
        if (userinfo != null) {
            //生成oken
            //JWT
            HashMap<String, Object> map = new HashMap<>();
            map.put("uid", userinfo.getUserid());
            String jwtToken = JwtUtils.createJwtToken(map);
            System.out.println("jwtToken = " + jwtToken);
            return  new R().add("token", jwtToken);
        }
      //登录失败
        return new R().setMessage("登录失败").setCode(500);
    }

    /**
     * 用户注册(新增)
     * @param userinfo
     * @return
     */
    @Override
    public String insertUsert(Userinfo userinfo) {
        return userinfoMapper.insert(userinfo)>0?"注册成功":"注册失败";
    }

    /**
     * 查询用户ID最大值
     * @return 最大值
     */
    @Override
    public int selectMax() {
        return userinfoMapper.selectMax();
    }

    /**
     * 新增时查找是否有相同手机号
     * @param phone 手机号
     * @return true/false
     */
    @Override
    public boolean selectPhone(String phone) {
        int i = userinfoMapper.selectPhone(phone);
        System.out.println("i = " + i);
        return userinfoMapper.selectPhone(phone)>0?false:true;
    }

    /**
     * 登录时查找是否有相同名称
     * @param name 姓名
     * @return true/false
     */
    @Override
    public boolean selectName(String name) {
        int i = userinfoMapper.selectName(name);
        System.out.println("i = " + i);
        return userinfoMapper.selectName(name)>0?false:true;
    }

    /**
     * 根据ID获取用户信息
     * @param uid 用户ID
     * @return 用户类
     */
    @Override
    public Userinfo selectByUserid(int uid) {
        QueryWrapper<Userinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("userid", uid);
        return userinfoMapper.selectOne(wrapper);
    }

    /**
     * 查询所有的用户
     * @return 集合
     */
    @Override
    public List<Userinfo> selectAllUser(String username) {
        QueryWrapper<Userinfo> wrapper = new QueryWrapper<>();
        if (!username.equals("")) {
            wrapper.like("username", username).or().like("phone", username);
        }
        return userinfoMapper.selectList(wrapper);
    }


}
